import dayjs from "dayjs"
import { removeConfigLayout } from "@/utils/cache/local-storage"
import { constantRoutes, asyncRoutes } from "@/router/index"

/** 格式化时间 */
export const formatDateTime = (time: string | number | Date) => {
  return time ? dayjs(new Date(time)).format("YYYY-MM-DD HH:mm:ss") : "N/A"
}

/** 用 JS 获取全局 css 变量 */
export const getCssVariableValue = (cssVariableName: string) => {
  let cssVariableValue = ""
  try {
    // 没有拿到值时，会返回空串
    cssVariableValue = getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
  } catch (error) {
    console.error(error)
  }
  return cssVariableValue
}

/** 用 JS 设置全局 CSS 变量 */
export const setCssVariableValue = (cssVariableName: string, cssVariableValue: string) => {
  try {
    document.documentElement.style.setProperty(cssVariableName, cssVariableValue)
  } catch (error) {
    console.error(error)
  }
}

/** 重置项目配置 */
export const resetConfigLayout = () => {
  removeConfigLayout()
  location.reload()
}

// 日志管理系统模块筛选路由模块方法
export const screenRouter = () => {
  const isConstantRoutes = constantRoutes.filter(
    (item: any) => item.name && !["Session", "Screen", "Empower", "Repassword"].includes(item.name)
  )
  const originalRouter = [...isConstantRoutes, ...asyncRoutes] // 改成动态路由用这个
  // const originalRouter = [...isConstantRoutes]
  return originalRouter
}
// 下载本地模板
export const downloadStaticTemplate = (href: string, name: string) => {
  let a = document.createElement("a")
  a.href = href
  a.download = name
  a.style.display = "none"
  document.body.appendChild(a)
  a.click()
  a.remove()
}
